package cn.itsource.org.controller;

import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.base.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.service.IDepartmentService;
import cn.itsource.base.util.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @ApiOperation(value = "保存一个部门" )
    @PutMapping("/save")
    public AjaxResult save(@Valid @RequestBody Department department){
        if(department.getId()==null){
            departmentService.save(department);
        }else {
            departmentService.update(department);
        }
        return AjaxResult.me();
    }

    @ApiOperation(value = "根据id删除一个部门" )
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        departmentService.deleteById(id);
        return AjaxResult.me();
    }

    @ApiOperation(value = "批量删除部门" )
    @PatchMapping("/deleteAll")
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        departmentService.batchDeleteByIds(ids);
        return AjaxResult.me();
    }

    @ApiOperation(value = "根据id查询部门" )
    @GetMapping("/{id}")
    public AjaxResult selectOne(@PathVariable("id") Long id){
        Department department = departmentService.selectById(id);
        return AjaxResult.me().setResultObj(department);
    }

    @ApiOperation(value = "查询全部部门" )
    @GetMapping("/all")
    public AjaxResult selectAll(){
        List<Department> departments = departmentService.selectAll();
        return AjaxResult.me().setResultObj(departments);
    }

    @ApiOperation(value = "分页查询部门" )
    @PostMapping("/pageList")
    public AjaxResult pageList(@RequestBody DepartmentQuery departmentQuery){
        PageList<Department> departmentPageList = departmentService.queryPage(departmentQuery);
        return AjaxResult.me().setResultObj(departmentPageList);
    }

    @GetMapping("/getDeptTree")
    public AjaxResult getDeptTree(){
        return AjaxResult.me().setResultObj(departmentService.getDepartmentTree());
    }
}
